package com.wzh.yebServe.controller;

import com.wzh.yebServe.pojo.ChatMsg;
import com.wzh.yebServe.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author wzh
 * @create 2022/11/22 19:37
 * @Description:
 */
@Controller
public class WsController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;


    @MessageMapping("/ws/chat")
    public void handMsg(Authentication authentication, ChatMsg chatMsg){
        User user =(User) authentication.getPrincipal();
        chatMsg.setFrom(user.getUsername());
        chatMsg.setFormNickName(user.getName());
        chatMsg.setDate(LocalDateTime.now());
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);
    }
}
